<template>
  <div class="app-container">
    <div class="header">
      <div class="word">
        <div class="w1">
          地址管理
        </div>
        <div class="w2">
          在地址管理中你可以查看收件人，收获地址等基本信息，同时还可以实现查询用户地址，删除用户地址等功能，提供一套可行的地址管理方案
        </div>
        <div class="w2">
          地址管理系统是后台系统中重要的一环,它可以为我们提供用户地址的信息，为进一步销售做准备
        </div>
      </div>
      <img class="imgDri" src="../static/u=4093097785,1812418156&fm=253&fmt=auto&app=138&f=JPEG.webp">
    </div>
    <div class="index">
      <div class="searchHead">
        <el-button type="primary" @click="dialogFormVisible = true">新增地址</el-button>
        <el-input v-model="input3" placeholder="请输入用户编号" class="searchIuput">
          <el-button slot="append" v-model="searchInf" icon="el-icon-search" @click="searchMes" />
        </el-input>
      </div>

      <el-dialog title="销售信息添加" :visible.sync="dialogFormVisible">
        <el-form>
          <el-form-item class="item" label="收件人" :label-width="formLabelWidth">
            <el-input v-model="addMes.receiveName" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="收件人id" :label-width="formLabelWidth">
            <el-input v-model="addMes.userId" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="收件人电话" :label-width="formLabelWidth">
            <el-input v-model="addMes.phone" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="地址" :label-width="formLabelWidth">
            <el-input v-model="addMes.address" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="邮编" :label-width="formLabelWidth">
            <el-input v-model="addMes.postalCode" class="in" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addDispatchOrderLogistics()">确 定</el-button>
        </div>
      </el-dialog>

      <el-table :data="mes" type="" style="width: 100%">
        <el-table-column prop="receiveName" label="收件人" />
        <el-table-column prop="userId" label="收件人id" />
        <el-table-column prop="phone" label="收件人电话" />
        <el-table-column prop="addressId" label="地址id" />
        <el-table-column prop="address" label="地址" />
        <el-table-column prop="gmtCreate" label="创建时间" />
        <el-table-column prop="postalCode" label="邮政编号" />
        <el-table-column prop="carStatus" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="mes[scope.$index].isShow = true">修改</el-button>
            <el-dialog :modal-append-to-body="false" title="修改信息" :visible.sync="mes[scope.$index].isShow">
              <el-form class="form">
                <el-form-item class="item" label="用户名" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].receiveName" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="手机号" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].phone" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="地址" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].address" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="邮编" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].postalCode" autocomplete="off" />
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="mes[scope.$index].isShow = false">取 消</el-button>
                <el-button type="primary" @click="saleChange(scope.$index)">确 定</el-button>
              </div>
            </el-dialog>
            <el-button
              type="primary"
              :disabled="mes[scope.$index].isDelshow"
              @click="saleDel(scope.$index)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :page-size="5"
          layout="prev, pager, next, jumper"
          :total="pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import request from '../../utils/request'
import vueQr from 'vue-qr'
export default {
  name: 'CarLogistics',
  components: {
    vueQr
  },
  data() {
    return {
      saleId: '',
      mes: [],
      dialogTableVisible: false,
      formLabelWidth: '100px',
      tranceData: 'http://localhost:9527/#/saleAll',
      addMes: {

        'address': '',
        'addressId': 0,
        'isDelete': 0,
        'phone': '',
        'postalCode': 0,
        'receiveName': '',
        'userId': 0
      },
      dialogFormVisible: false,
      pageSize: null,
      select: '',
      searchInf: '',
      input3: '',
      page: 1
    }
  },
  async mounted() {
    this.logisticsOrderLogistics()
  },
  methods: {
    async saleDel(index) {
      await request({
        method: 'delete',
        url: '/mall/address/deleteAddress/' + this.mes[index].addressId
      }).then((response) => {
        if (response.statusCode == 20000) {
          this.logisticsOrderLogistics()
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，修改失败',
            type: 'warning'
          })
        }
      })
    },
    async saleChange(index) {
      await request({
        method: 'post',
        url: '/mall/address/updateAddress',
        data: this.mes[index]
      }).then((response) => {
        console.log(response)
        if (response.statusCode == 20000) {
          this.$message({
            message: '卖出成功',
            type: 'success'
          })
          this.logisticsOrderLogistics()
        } else {
          this.$message({
            message: '发生错误，修改失败',
            type: 'warning'
          })
        }
      })
    },
    async logisticsOrderLogistics() {
      await request({
        method: 'get',
        url: '/mall/address/pageAddress/' + this.page + '/5'
      }).then((response) => {
        console.log(response)
        this.mes = response.data.records
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isDelShow', false)
        }
        this.pageSize = response.data.total
      })
    },
    async addDispatchOrderLogistics() {
      request({
        method: 'post',
        url: '/mall/address/addAddress',
        data: this.addMes
      }).then(response => {
        console.log(response)
        console.log(this.saleId)
        if (response.statusCode == 20000) {
          this.dialogFormVisible = false
          this.logisticsOrderLogistics()
          this.addMes = {}
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，添加失败',
            type: 'warning'
          })
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.page = val
      console.log(this.page, 'pagepage')
      request({
        method: 'get',
        url: '/mall/address/pageAddress/' + this.page + '/5'
      }).then((response) => {
        console.log(response)
        this.mes = response.data.records
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isDelShow', false)
          if (this.mes[i].saleStatus == '未销售') {
            this.$set(this.mes[i], 'isSaleShow', false)
          } else {
            this.$set(this.mes[i], 'isSaleShow', true)
          }
        }
        this.pageSize = response.data.total
      })
    },
    searchMes() {
      request({
        url: '/mall/address/getAllById/' + this.input3,
        method: 'get'
      }).then(response => {
        if (response.statusCode == 20000) {
          this.$message({
            message: '查询成功',
            type: 'success'
          })
          this.mes = response.data
          this.pageSize = 1
        } else {
          this.$message({
            message: '查无此人，查询失败',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.qrcode-container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header {
    width: 100%;
    height: 350px;
    // border: 1px solid black;
    display: flex;
    margin-bottom: 20px;
    justify-content: space-between;
}

.form {
    display: flex;
    flex-wrap: wrap;
    // justify-content: space-around;
}

.sel {
    width: 310px;
    display: flex;
    // border: 1px solid black;
    justify-content: right;
    align-content: center;
}

.sel2 {
    width: 240px;
}

.page {
    width: 100%;
    display: flex;
    justify-content: right;
}

.imgDri {
    height: 300px;
    width: 400px;
}

.item {}

.w1 {
    height: 80px;
    font-size: 25px;
    font-weight: 350;
}

.w2 {
    height: 50px;
    font-size: 15px;
    font-weight: 250;
}

.searchHead {
    display: flex;
    justify-content: space-between;
}

.searchIuput {
    width: 350px;
}

.sel3 {
    width: 100px;
}
</style>
